//
//  CoinDetialView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/30.
//

#import "CoinDetialView.h"
#import "CoinDetialListCell.h"
#import "ScoreDetialManager.h"
#import "ScoreDetialListModel.h"

@interface CoinDetialView()<UITableViewDelegate,UITableViewDataSource,JPAPIManagerCallBackDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)ScoreDetialManager *detialManager;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIView *topView;

@end

@implementation CoinDetialView

-(ScoreDetialManager *)detialManager{
    if(!_detialManager){
        _detialManager = [[ScoreDetialManager alloc]init];
        _detialManager.delegate = self;
    }
    return _detialManager;
}

-(NSMutableArray *)dataArr{
    if(!_dataArr){
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        [self createUI];
        [self loadData];
    }
    return self;
}

-(void)loadData{
    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if(userId){
        self.detialManager.userId = userId;
        [self.detialManager loadData];
    }
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    
    __weak typeof(self)self_weak_ = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [self_weak_ removeFromSuperview];
    }];
    [self addGestureRecognizer:tap];
    
    CGFloat h600 = [DeviceUtils getAdaptedHeightForDesignHeight:600 andCurrentAvailableHeight:self.currentAvailableHeight];
    CGFloat h119 = [DeviceUtils getAdaptedHeightForDesignHeight:600 andCurrentAvailableHeight:self.currentAvailableHeight];
    if((h600+h119*2)>self.currentAvailableHeight){
        h119 = (self.currentAvailableHeight-h600)/2.0;
    }
    
    UIView *bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h600);
        make.left.mas_equalTo(@24);
        make.right.mas_equalTo(@-24);
        make.top.mas_equalTo(h119);
    }];
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor clearColor];
    topView.backgroundColor = kCoinDetialBgColor(self.currentAvailableWidth-48,150);
    [bgView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(@0);
        make.height.mas_equalTo(@150);
    }];
    self.topView = topView;
    
    //挡住渐变图底下多出来的线
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@2);
        make.bottom.mas_equalTo(topView.mas_bottom).offset(1);
        make.left.right.mas_equalTo(@0);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(16);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.text = @"岛币明细";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@20);
        make.height.mas_equalTo(@22);
        make.centerX.mas_equalTo(bgView);
        make.width.mas_equalTo(@100);
    }];
    
    self.tipLabel = [[UILabel alloc]init];
    self.tipLabel.font = JPUIFontPingFangSCRegular(13);
    self.tipLabel.textColor = KFontGrayColor;
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    self.tipLabel.backgroundColor = [UIColor clearColor];
    self.tipLabel.text = @"*以上为最近0条岛币明细";
    [bgView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-10);
        make.height.mas_equalTo(@18);
        make.centerX.mas_equalTo(bgView);
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-68);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,62,self.currentAvailableWidth-48,h600-62-38) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 63;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:NSClassFromString(@"CoinDetialListCell") forCellReuseIdentifier:@"CoinDetialListCell"];
    [bgView addSubview:self.tableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self_weak_.dataArr removeAllObjects];
        self_weak_.tipLabel.text = @"*以上为最近0条岛币明细";
        [self_weak_ loadData];
    }];
    
    self.bgView = bgView;
    
}

-(void)refreshWidth{
    __weak typeof(self)self_weak_ = self;
    CGFloat h600 = [DeviceUtils getAdaptedHeightForDesignHeight:600 andCurrentAvailableHeight:self.currentAvailableHeight];
    CGFloat h119 = [DeviceUtils getAdaptedHeightForDesignHeight:600 andCurrentAvailableHeight:self.currentAvailableHeight];
    if((h600+h119*2)>self.currentAvailableHeight){
        h119 = (self.currentAvailableHeight-h600)/2.0;
    }
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h600);
        make.top.mas_equalTo(h119);
    }];
    self.topView.backgroundColor = kCoinDetialBgColor(self.currentAvailableWidth-48,150);
    [self.tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self_weak_.currentAvailableWidth-68);
    }];
    self.tableView.frame = CGRectMake(0,62,self.currentAvailableWidth-48,h600-62-38);
    [self.tableView reloadData];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CoinDetialListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoinDetialListCell"];
    if(indexPath.row < self.dataArr.count){
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

#pragma mark - JPAPIManagerCallBackDelegate
-(void)managerCallDLDLAPIDidSuccess:(JPAPIBaseManager *)manager{
    
    if([manager isEqual:self.detialManager]){
        NSDictionary *dic = manager.response.content;
        NSArray *arr = [ScoreDetialListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]];
        self.dataArr = [NSMutableArray arrayWithArray:arr];
        NSString *count = [NSString stringWithFormat:@"%@",dic[@"data"][@"count"]];
        self.tipLabel.text = [NSString stringWithFormat:@"*以上为最近%@条岛币明细",count];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }
    
}

-(void)managerCallDLDLAPIDidFailed:(JPAPIBaseManager *)manager{
    [self.tableView.mj_header endRefreshing];
    if(manager.errorMessage){
        [JPProgressHUD showMsg:manager.errorMessage];
    }
}



@end
